package com.atguigu.tingshu.order.startegy;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderDerateService;
import com.atguigu.tingshu.order.service.OrderDetailService;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 保存订单接口
 */
public interface SaveOrderStrategy {

    // 定义保存订单的接口方法
    public abstract OrderInfoVo saveOrder(OrderInfoVo orderInfoVo);

    // 定义获取支付方式的方法
    public abstract String getPayWay();

    // 保存订单的接口方法
    default public void saveOrderAndDetailAndOrderDerate(OrderInfoVo orderInfoVo,
                                                         OrderInfoService orderInfoService,
                                                         OrderDetailService orderDetailService,
                                                         OrderDerateService orderDerateService) {
        //获取当前用户登录的id
        Long userId = AuthContextHolder.getUserId();

        //1.保存订单的基本数据
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(userId);
        List<OrderDetailVo> orderDetailVoList = orderInfoVo.getOrderDetailVoList();
        String itemName = orderDetailVoList.get(0).getItemName();

        orderInfo.setOrderTitle(itemName);
        orderInfo.setOrderNo(orderInfoVo.getTradeNo());
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_UNPAID);
        orderInfo.setOriginalAmount(orderInfoVo.getOriginalAmount());
        orderInfo.setDerateAmount(orderInfoVo.getDerateAmount());
        orderInfo.setOrderAmount(orderInfoVo.getOrderAmount());
        orderInfo.setItemType(orderInfoVo.getItemType());
        orderInfo.setPayWay(orderInfoVo.getPayWay());
        orderInfoService.save(orderInfo);

        //2.保存订单的明细数据
        List<OrderDetail> collect = orderDetailVoList.stream().map(orderDetailVo -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderInfo.getId());
            orderDetail.setItemId(orderDetailVo.getItemId());
            orderDetail.setItemName(orderDetailVo.getItemName());
            orderDetail.setItemUrl(orderDetailVo.getItemUrl());
            orderDetail.setItemPrice(orderDetailVo.getItemPrice());
            return orderDetail;
        }).collect(Collectors.toList());
        orderDetailService.saveBatch(collect);
        //3.保存订单减免数据
        List<OrderDerateVo> orderDerateVoList = orderInfoVo.getOrderDerateVoList();
        if (!CollectionUtils.isEmpty(orderDerateVoList)) {
            List<OrderDerate> orderDerateList = orderDerateVoList.stream().map(orderDerateVo -> {
                OrderDerate orderDerate = new OrderDerate();
                orderDerate.setOrderId(orderInfo.getId());
                orderDerate.setDerateType(orderDerateVo.getDerateType());
                orderDerate.setDerateAmount(orderDerateVo.getDerateAmount());
                orderDerate.setRemarks(orderDerate.getRemarks());
                return orderDerate;
            }).collect(Collectors.toList());
            orderDerateService.saveBatch(orderDerateList);
        }
        //执行后续操作
        this.saveOrderProcess(orderInfoVo.getTradeNo());
    }
    // 定义保存订单数据后的抽象方法
    public abstract void saveOrderProcess(String orderNo);
}
